package com.flex.tools
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import mx.controls.Alert;
	import mx.controls.Image;
	import spark.primitives.BitmapImage;
	public class Tools
	{
		var max:uint;
		var min:uint;
		var list:Array=new Array();
		var level:Array;
		public function Tools()
		{
		}
		public function setLevel(colors:Array):void{
			this.level=colors;
		}
		private function generateColor(town:String):uint{
			var colors:Array=ColorGradient.generateTransitionalColor();
			for(var i in list){
				var sr:SumReport=list[i];
				if(town==sr.getTown()){
					var loss:Number=sr.getMoneyLoss();
					//Alert.show(loss.toString());
					if(loss<level[0]){
						return colors[0];
					}else if(loss>=level[9]){
						return colors[11];
					}else{
						for(var j:int=1;j< 10;j++){
							if(loss>level[j-1]&&loss<=level[j]){
								return colors[j+1];
							}
						}
					}
				}
			}
			return 0xffffff;
		}
		public function setImageBgcolor(town:String,image:Image):void {
			var bmd:BitmapData = new BitmapData(image.width, image.height,true,0x00FFFFFF);
			var bm:Bitmap=image.content as Bitmap;
			bmd.draw(bm.bitmapData);
			var height:uint=image.height;
			var width:uint=image.width;
			var color:uint=generateColor(town);
			for(var j:uint = 0; j<height; j++){
				for(var i:uint = 0; i<width; i++){
					var cor:uint=bmd.getPixel(i,j);
					if(16711422==cor){
						bmd.setPixel(i,j,color);
					}
				}
			}
			var bm2=new Bitmap(bmd);
			image.source=bm2;
		}
		public function changToFlexList(sumReportsList:Array,flag:int){
			for(var i in sumReportsList){
				var sr:SumReport=new SumReport();
				sr.setTown(sumReportsList[i].town);
				if(flag==2){
					sr.setMoneyLoss(sumReportsList[i].jeconomyloss);
				}else if(flag==3){
					sr.setMoneyLoss(sumReportsList[i].neconomyloss+sumReportsList[i].leconomyloss+sumReportsList[i].yeconomyloss);
				}else{
					//Alert.show(sumReportsList[i].moneyLoss);
					sr.setMoneyLoss(sumReportsList[i].moneyLoss);
				}
				list.push(sr);
			}
			list=list.sort(sortOnMoneyLoss);
			max=(list[0] as SumReport).getMoneyLoss();
			min=(list[list.length-1] as SumReport).getMoneyLoss();
		}
		internal function sortOnMoneyLoss(obj1:SumReport,obj2:SumReport):Number{
			var aPrice:uint = obj1.getMoneyLoss();
			var bPrice:uint = obj2.getMoneyLoss();
			if(aPrice > bPrice) {
				return 1;
			} else if(aPrice < bPrice) {
				return -1;
			} else  {
				return 0;
			}
		}
		
	}
}